Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

61
Views
JavaScript convert [object Object] to JSON

I am implementing a token based authentication and I need to access the token I have set when i check if the user is authenticated.

I am using this method to check if the user is authenticated:

function isAuthenticated(req, res, next) {
    const token = req.headers['authorization'];
    console.log(token);
    if (token) {
        req.token = token;
        next();
    } else {
        res.sendStatus(401);
    }
}

The console.log(token) prints out [object Object] how can I convert this to a json object?

The token is generated with the jsonwebtoken module this way:

jwt.sign({ user_id: user.user_id }, config.app.secretKey, { expiresIn: 60 * 60 * 24 * 7 }, (err, token) => {
    return res.send(token);
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Unfortunately, the string "[object Object]" doesn't have enough information to recreate back the original object. You'll have to debug the logic and find out why req.headers['authorization'] has such value in the first place.

about 4 years ago · Juan Pablo Isaza Report

0

Use JSON.stringify

function isAuthenticated(req, res, next) {
    const token = req.headers['authorization'];
    console.log(JSON.stringify(token));
    if (token) {
        req.token = token;
        next();
    } else {
        res.sendStatus(401);
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!